#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <cmath>
using namespace std;
#define ldb long double
#define ll long long
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define ff first
#define ss second
#define nextLine() {int c = 0; while ((c = getchar()) != 10 && c != EOF); }
const ldb pi = fabsl(atan2l(0.0, -1.0));
const ldb eps = 1e-9;
const int inf = 1 << 28;


string s;
int k;
int n;
int a[27][26];
int dp[110][110][27];

void load()
{
	cin >> s >> k;
	cin >> n;
	char c1, c2;
	int d;
	for (int i = 0; i < n; i++)
	{
		nextLine();
		c1 = getchar();
		getchar();
		c2 = getchar();
		scanf("%d", &d);
		c1 -= 'a';
		c2 -= 'a';
		a[c1][c2] = d;
		
    }
}

int f(int i, int j, int c)
{
	if (j < 0) return -inf;
	if (i == s.size())
	{
		return 0;
	}
	if (dp[i][j][c] != -1) return dp[i][j][c];
	dp[i][j][c] = f(i + 1, j, s[i] - 'a') + a[c][s[i] - 'a'];
	for (int l = 0; l < 26; l++)
	{
		dp[i][j][c] = max(dp[i][j][c], f(i + 1, j - 1, l) + a[c][l]);
	}
	return dp[i][j][c];
}

void solve()
{
	memset(dp, -1, sizeof dp);
	cout << f(0, k, 26);
}

#define file "c"

int main()
{
	#ifndef ONLINE_JUDGE
		freopen(file".in", "rt", stdin);
		freopen(file".out", "wt", stdout);
  	#endif
  	load();
  	solve();
  	return 0;
}
